<?php

require_once 'lib.php';
require_once 'common.php';



/**
 * Moodle XMLDB implementation
 * 
 * @author Kirill Krasnoschekov
 */
class XMLDBGuestbook extends Guestbook
{
    // moodle_database object to access the database
    private $DB;
    
    // Table names
    private $msgTable;
    private $commentTable;
    
    
    
    /**
     * Constructor
     * 
     * @param moodle_database $DB 
     * Moodle database object
     * @param string $msgTable 
     * Messages table name
     * @param string $commentTable 
     * Comments table name
     * @author Kirill Krasnoschekov 
     */
    public function __construct
            (moodle_database $DB, 
            $msgTable = 'block_guestbook_messages',
            $commentTable = 'block_guestbook_comments')
    {
        $this->DB = $DB;
        $this->msgTable = $msgTable;
        $this->commentTable = $commentTable;
    }
    
    /**
     * Destructor: empty
     * 
     * @author Kirill Krasnoschekov 
     */
    public function __destruct() {}
    
    /**
     * Number of pages
     * 
     * @author Kirill Krasnoschekov 
     */
    public function CountPages()
    {
        return ceil
            ($this->DB->count_records($this->msgTable) / $this->messagesPerPage);
    }

    /**
     * Fetch messages on the given page
     * 
     * Pages are indexed from 0 to ($this->CountPages - 1).
     * Message: dictionary (id, name, text, timestamp)
     *
     * @param int $page 
     * Number of the page to fetch
     * @return array
     * List of messages fetched
     * @author Kirill Krasnoschekov 
     */    
    public function FetchMessages($page)
    {
        // Calculating message numbers on the given page
        $numStart = $page * $this->messagesPerPage;
        $numEnd = $numStart + $this->messagesPerPage;
        // Querying
        $records = $this->DB->get_records($this->msgTable, null, 
                'timestamp DESC', '*', $numStart, $this->messagesPerPage);

        // Converting records from objects into hashed arrays
        $recordsAssoc = array();
        foreach ( $records as $id => $record )
        {
            $recordsAssoc[] = array(
                    'id' => $id, 
                    'name' => $record->name,
                    'text' => $record->text,
                    'timestamp' => date('Y-m-d H:i:s', $record->timestamp));
        }
        return $recordsAssoc;
    }
    
    /**
     * Fetch comments to the given message
     * 
     * comment: dictionary (id, msgid, name, text, timestamp)
     * 
     * @param int $msgid
     * Message id for which comments are fetched
     * @return array
     * List of comments fetched
     * @author Kirill Krasnoschekov 
     */
    public function FetchComments($msgid) 
    {
        // Querying
        $records = $this->DB->get_records
                ($this->commentTable, array('msgid' => $msgid));
        // Converting records from objects into hashed arrays
        $recordsAssoc = array();
        foreach ( $records as $id => $record )
        {
            $recordsAssoc[] = array(
                    'id' => $id,
                    'msgid' => $msgid, 
                    'name' => $record->name,
                    'text' => $record->text,
                    'timestamp' => date('Y-m-d H:i:s', $record->timestamp));
        }
        return $recordsAssoc;
    }
    
    /**
     * Add message
     * 
     * @param $name User name
     * @param $text Message text 
     * @author Kirill Krasnoschekov 
     */
    public function AddMessage($name, $text)
    {
        // Forming the object
        $message = new stdClass;
        $message->name = $name;
        $message->text = $text;
        $message->timestamp = time();
        // Querying the database
        return $this->DB->insert_record($this->msgTable, $message);
    }
    
    /**
     * Add comment
     * 
     * @param integer $msgid Message id
     * @param string $name User name
     * @param string $text Comment text
     * @author Kirill Krasnoschekov 
     */
    public function AddComment($msgid, $name, $text)
    {
        // Checking if message does not exist
        if ( !$this->MessageExists($msgid) )
        {
            die(self::$err['MSGID_NOT_FOUND']);
        }
        // Forming the object
        $comment = new stdClass;
        $comment->name = $name;
        $comment->text = $text;
        $comment->msgid = $msgid;
        $comment->timestamp = time();
        // Querying
        return $this->DB->insert_record($this->commentTable, $comment);
    }
    
    /**
     * Delete message
     * 
     * @param int $msg_id ID of the message to delete
     * @author Kirill Krasnoschekov 
     */
    public function DeleteMessage($msgid) 
    {
        // Comments for this message
        $this->DB->delete_records
                ($this->commentTable, array('msgid' => $msgid));
        // Message
        return $this->DB->delete_records
                ($this->msgTable, array('id' => $msgid));
    }
    
    /**
     * Delete comment 
     * 
     * @param int $commentid ID of the comment to delete
     * @author Kirill Krasnoschekov 
     */
    public function DeleteComment($commentid) 
    {
        return $this->DB->delete_records
                ($this->commentTable, array('id' => $commentid));
    }
    
    /**
     * Update message
     * 
     * @param int $msgid Message ID
     * @param string $name New user name
     * @param string $text New message text
     * @author Kirill Krasnoschekov 
     */
    public function UpdateMessage($msgid, $name, $text)
    {
        // Forming the object
        $message = new stdClass;
        $message->id = $msgid;
        $message->name = $name;
        $message->text = $text;
        $message->timestamp = time();
        // Querying the database
        return $this->DB->update_record($this->msgTable, $message);
    }

    /**
     * MessageExists
     * 
     * @param int $msgid Message ID
     * @return True if yes, False if no
     * @author Kirill Krasnoschekov 
     */    
    protected function MessageExists($msgid)
    {
        return $this->DB->record_exists
                ($this->msgTable, array('id' => $msgid));
    }
    
    /**
     * CommentExists
     * 
     * @param int $commentid Comment ID
     * @return True if yes, False if no
     * @author Kirill Krasnoschekov 
     */    
    protected function CommentExists($commentid) 
    {
        return $this->DB->record_exists
                ($this->commentTable, array('id' => $commentid));
    }
}

?>